# GF_PY3_CLASS_Wrapper_Tushare.py
# Create by GF 2025-04-03 23:30

import tushare

# ##################################################

class set_api_key:

    def __init__(self):
    
        self.api_key = ''
    
    def update(self, key_string:str):
    
        self.api_key = key_string
        # ..........................................
        return self

class set_adjusted:

    def __init__(self):

        # "Tushare 1.4.x": tushare.pro_api( ... ).daily(adj:str, start_date:str, end_date:str)
        # - adj = None   # 不复权 (Default)
        # - adj = "qfq"  # 前复权
        # - adj = "hfq"  # 后复权
    
        self.adjusted = None
    
    def update(self, adjusted_string:str):
    
        self.adjusted = adjusted_string
        # ..........................................
        return self

class set_start_date:

    def __init__(self):
    
        # The Date Format Required by "Tushare 1.4.x" is "20250403".
    
        self.start_date = "20250403"
    
    def update(self, start_date_string:str):
    
        self.start_date = start_date_string
        # ..........................................
        return self

class set_end_date:

    def __init__(self):
    
        # The Date Format Required by "Tushare 1.4.x" is "20250403".
    
        self.end_date = "20250403"
    
    def update(self, end_date_string:str):
    
        self.end_date = end_date_string
        # ..........................................
        return self

class wrapper_tushare_1_4_x(object):

    # - If "adj = (Error Value)", Returns "df.empty == True" Without Generating Error.
    # - If "end_date < start_date", Returns "df.empty == True" Without Generating Error.
    # - If "(No Datas) in (start_date, ..., end_date)", Returns "df.empty == True" Without Generating Error.

    def __init__(self):

        self.api_key    = set_api_key()
        self.adjusted   = set_adjusted()
        self.end_date   = set_end_date()
        self.start_date = set_start_date()
        self.end_date   = set_end_date()

    def get_stock_daily(self):

        try:
        
            # try 语句包含可能引发异常的代码块
            # ......................................
            tushare_pro_api = tushare.pro_api(self.api_key.api_key)
            # ......................................
            df = tushare_pro_api.daily(
                adj = self.adjusted.adjusted, start_date = self.start_date.start_date, end_date = self.end_date.end_date
            )

        except Exception as e:
        
            # except 语句用于捕获并处理这些异常
            # ......................................
            print('{"error": "%s"}' % str(e))
            # ......................................
            return None

        else:
            
            # else 语句用于处理没有异常发生的情况
            # ......................................
            return df

# EOF Signed by GF.
